// pages/my/shop.js
let app = getApp();
const qiniuUploader = require("../../qiniu.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shop: {},
    hasShop: false,
    cates: [],
    catesTpl: [],
    cateIndex: 0,
    imgPath: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    app.request('api/wx/myShopInfo', {}, function(data){
      that.setData({
        shop: data ? data : {},
        hasShop: data ? true : false,
        // imgPath: data ? data.shop_thumb : ''
      });
      // app.globalData.hasShop = data ? true : false;
    });
    app.request('api/shop/cates', {}, (cates)=>{
      that.data.catesTpl = cates;
      let _cates = [];
      cates.forEach((item, index)=>{
        _cates.push(item.text);
      });
      that.data.shop.shop_cate_id = cates[0].value
      that.setData({
        cates: _cates
      });
    });
  },
  setShopInfo(key, value) {
    this.data.shop[key] = value
  },
  inputName: function(event){
    this.setShopInfo('shop_name', event.detail.value);
  },
  inputAddress(event){
    this.setShopInfo('shop_address', event.detail.value);
  },
  inputMobile(event){
    this.setShopInfo('shop_mobile', event.detail.value);
  },
  inputSummary(event){
    this.setShopInfo('shop_summary', event.detail.value);
  },
  selectPoi: function(){
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res);
        let shop = that.data.shop;
        let lng = res.longitude.toFixed(6),
          lat = res.latitude.toFixed(6);
        shop.shop_lon = lng
        shop.shop_lat = lat
        shop.shop_address = res.address
        that.setData({
          shop: shop
        }); 
        wx.request({
          url: 'http://restapi.amap.com/v3/geocode/regeo',
          data: {
            key: '8bbfb9bba0d8f88544f0fce11cbeb3f2',
            location: lng + "," + lat
          },
          success(response) {
            let location = response.data;
            that.data.shop.shop_area_id = location.regeocode.addressComponent.adcode
          }
        })
      }
    });
  },
  getAdcode(){
    let that = this;
    console.log(that.data.shop.lon + "," + that.data.shop.lat)
  },
  submit(){
    let that = this;
    if (that.data.chooseImg) {
      qiniuUploader.upload(that.data.imgPath, (res)=>{
        that.data.shop.shop_thumb = res.imageURL
        that.setData({
          chooseImg: false
        })
        that.doSubmit();
      }, () => { }, {
          uploadURL: 'https://upload.qiniup.com',
          uptoken: app.globalData.upToken,
          region: 'z0',
          domain: '',
      });
    } else {
      that.doSubmit();
    }
  },
  doSubmit(){
    app.request('api/shop/save', this.data.shop, (data, msg)=>{
      wx.showToast({
        title: msg,
      })
      wx.navigateBack({
        delta: 1
      })
    }, (msg)=>{
      wx.showToast({
        title: msg,
      })
    });
  },
  bindPickerChange(obj){
    this.setData({
      cateIndex: obj.detail.value 
    });
    this.data.shop.shop_cate_id = that.data.catesTpl[obj.detail.value].value;
  },
  selImg(){
    let that = this
    wx.chooseImage({
      count: 1,
      success(imgPath){
        that.setData({
          chooseImg: true,
          imgPath: imgPath.tempFilePaths[0]
        })
      }
    });
  }
})